import { Command } from '../constant';
import Taro from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';
export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      const taskList = await api.TaskCenterController.getTaskList({});
      const activation = await api.TaskCenterController.getActivation({});
      action.commonChange('main.taskList', taskList);
      action.commonChange(
        'main.process',
        parseInt(`${(activation.completedActivation / activation.sumActivation) * 100}`),
      );
      const processText = Math.round(activation.completedActivation / activation.sumActivation * 100).toFixed(2)
      action.commonChange('main.processText', processText)
    },

    async goFinish(e: any) {
      const task = await api.TaskCenterController.getTask(e.id);
      if (task.taskReleaseVO && task.taskReleaseVO.pagePath && task.taskReleaseVO.pagePath.startsWith('/pages')) {
        // task.taskReleaseVO.pagePath
        const reg = /package-(.)/;
        if (reg.test(task.taskReleaseVO.pagePath)) {
          Taro.navigateTo({
            url: task.taskReleaseVO.pagePath,
          });
        } else {
          Taro.switchTab({
            url: task.taskReleaseVO.pagePath,
          });
        }
      }
      Taro.setStorageSync('task', JSON.stringify(task));
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageATaskCenterMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
